package com.zbf.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zbf.common.ResponseResult;
import com.zbf.common.ResultStatus;
import com.zbf.dao.HtongDao;
import com.zbf.service.HtongService;
import com.zbf.utils.PdfUtils;
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2021/6/9  19:41
 * 描述:
 */
@RestController
public class HtongCOntroller {


    @Autowired
    HtongService htongService;

    @Autowired
    private MinioClient minioClient;

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    /**
     * 作者: LCG
     * 日期: 2021/6/9  19:42
     * 描述: 保存合同数据同时上传minio  同时做合同签章
     * @Param []
     * @Return com.zbf.common.ResponseResult
     */
    @RequestMapping("saveHt")
    public ResponseResult saveHt(@RequestBody Map<String,Object> map) throws Exception {

        //防止重复提交

        //生成合同
        Map<String, String> saveht = htongService.saveht(map);

        ResponseResult responseResult = ResponseResult.getResponseResult().setCode(ResultStatus.AUTH_SUCCESS.getCode())
                .setMsg(ResultStatus.AUTH_SUCCESS.getMsg()).setResult(saveht);

        return responseResult;

    }

    @Autowired
    private HtongDao htongDao;

    
    /**
     * 作者: LCG
     * 日期: 2021/6/10  11:55
     * 描述: 合同下载
     * @Param [htcode, response]
     * @Return void
     */
    @RequestMapping("xiazai/{htcode}")
    public void yulan(@PathVariable("htcode") String htcode, HttpServletResponse response) throws Exception {

        Map<String, String> map = htongDao.gethtInfo(htcode);

        PdfUtils.xizai(minioClient,map.get("filePath"),map.get("fileName"),"images2",response);

    }



    /**
     * 作者: LCG
     * 日期: 2021/6/10  11:55
     * 描述: 实名认证
     * @Param [map]
     * @Return com.zbf.common.ResponseResult
     */
    @RequestMapping("shimingrenzheng")
    public ResponseResult shimingrenzheng(@RequestBody Map<String,Object> map) throws IOException {

        String json = PdfUtils.remoteServer(map, "http://localhost:20000/shimingRenZheng");

        JSONObject jsonObject = JSON.parseObject(json);

        if(jsonObject.get("code").toString().equals("200")){
           return ResponseResult.getResponseResult().setCode(200).setMsg(jsonObject.get("result").toString()).setResult(json);
        }else{
           return ResponseResult.getResponseResult().setCode(500).setMsg(jsonObject.get("result").toString()).setResult(json);
        }

    }


    @RequestMapping("renzhengHetong")
    public ResponseResult renzhengHetong(@RequestParam String htcode) throws IOException {

        //查询合同信息
        Map<String, String> info = htongDao.gethtInfo(htcode);
        //调用认证的接口
        Map<String,Object> map=new HashMap<>();
        map.put("htcode",info.get("htcode"));
        map.put("content",info.get("content"));
        String server = PdfUtils.remoteServer(map, "http://localhost:20000/renZhengQianZhang");
        JSONObject jsonObject = JSON.parseObject(server);

        return ResponseResult.getResponseResult().setCode(200).setMsg(jsonObject.get("msg").toString()).setResult(jsonObject.get("result"));
    }

}
